package com.wms.controller;

import com.wms.dto.SupplierDto;
import com.wms.pojo.PageResult;
import com.wms.pojo.Result;
import com.wms.pojo.Supplier;
import com.wms.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    //条件分页查询
    @GetMapping
    public Result getSupplierAll(SupplierDto dto){
        PageResult page = supplierService.getPage(dto);
        return Result.success(page);
    }

    //添加数据
    @PostMapping
    public Result saveSupplier(@RequestBody Supplier supplier){
        supplier.setCreateTime(LocalDateTime.now());
        supplier.setUpdateTime(LocalDateTime.now());
        supplierService.save(supplier);
        return Result.success();
    }

    //回显数据
    @GetMapping("/{id}")
    public Result getSupplierById(@PathVariable Integer id){
        Supplier supplier = supplierService.getById(id);
        return Result.success(supplier);
    }

    //修改数据
    @PutMapping
    public Result updateById(@RequestBody Supplier supplier){
        supplier.setUpdateTime(LocalDateTime.now());
        supplierService.updateById(supplier);
        return Result.success();
    }

    //删除数据
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        supplierService.removeById(id);
        return Result.success();
    }

    //查询所有供应商
    @GetMapping("/all")
    public Result getAll(){
        List<Supplier> list = supplierService.list();
        return Result.success(list);
    }

}
